//
//  LJHAlbumListTableViewCell.m
//  HEARME
//
//  Created by liujinhui on 16/1/28.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LJHAlbumListTableViewCell.h"

@implementation LJHAlbumListTableViewCell
- (void)dealloc
{
    [_contentCount release];
    [_newsDigest release];
    [_newsPic release];
    [_newsTitle release];
    [_nextBtn release];
    [super dealloc];
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createCell];
    }
    
    return self;
}

-(void)createCell
{
    self.newsPic = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH*0.02,WIDTH*0.02, WIDTH*0.24, WIDTH*0.24) ];
    self.newsPic.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.newsPic];
    [_newsPic release];
    
    self.newsTitle = [[LJHBaseLable alloc] initWithFrame:CGRectMake(self.newsPic.frame.origin.x + self.newsPic.frame.size.width + HEIGHT*0.01, self.contentView.bounds.origin.y + HEIGHT*0.02, WIDTH *0.6, HEIGHT*0.03)];
    self.newsTitle.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.newsTitle];
    [_newsTitle release];
    
    self.newsDigest = [[LJHBaseLable alloc] initWithFrame:CGRectMake(self.newsPic.frame.origin.x + self.newsPic.frame.size.width + HEIGHT*0.01 ,self.newsTitle.frame.origin.y + self.newsTitle.frame.size.height + HEIGHT*0.01, WIDTH *0.6, HEIGHT*0.03)];
    self.newsDigest.backgroundColor = [UIColor clearColor];
    self.newsDigest.font = [UIFont systemFontOfSize:13 weight:0];
    [self.contentView addSubview:self.newsDigest];
//    self.newsDigest.textColor = [UIColor lightGrayColor];
    self.newsDigest.numberOfLines = 2;
    [_newsDigest release];
    
    UIImageView *iconImageV = [[UIImageView alloc] initWithFrame:CGRectMake(self.newsPic.frame.origin.x + self.newsPic.frame.size.width + HEIGHT*0.01, self.newsDigest.frame.origin.y + self.newsDigest.frame.size.height + HEIGHT*0.015, HEIGHT*0.02, HEIGHT*0.02)];
    iconImageV.backgroundColor = [UIColor clearColor];
    iconImageV.image = [UIImage imageNamed:@"sound_playtimes@2x.png"];
    [self.contentView addSubview:iconImageV];
    [iconImageV release];
    
    //播放人数
    self.contentCount = [[LJHBaseLable alloc] initWithFrame:CGRectMake(self.newsPic.frame.origin.x + self.newsPic.frame.size.width + HEIGHT*0.04 , self.newsDigest.frame.origin.y + self.newsDigest.frame.size.height + HEIGHT*0.013, WIDTH*0.2, HEIGHT*0.03)];
    self.contentCount.backgroundColor = [UIColor clearColor];
    self.contentCount.font = [UIFont systemFontOfSize:13 weight:0];
    [self.contentView addSubview:self.contentCount];
    self.contentCount.textAlignment = NSTextAlignmentRight;
    //self.contentCount.textColor = [UIColor lightGrayColor];
    [_contentCount release];
    
    
    
    
    UIImageView *iconImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.contentCount.frame.origin.x + self.contentCount.frame.size.width + HEIGHT*0.01, self.contentCount.frame.origin.y, self.contentCount.frame.size.height, self.contentCount.frame.size.height)];
    iconImage.backgroundColor = [UIColor clearColor];
    iconImage.image = [UIImage imageNamed:@"album_tracks@2x.png"];
    [self.contentView addSubview:iconImage];
    [iconImage release];
    
    //集数
    self.duration = [[LJHBaseLable alloc] initWithFrame:CGRectMake(self.contentCount.frame.origin.x + self.contentCount.frame.size.width + HEIGHT*0.04 , self.contentCount.frame.origin.y , WIDTH*0.08, HEIGHT*0.03)];
    self.duration.backgroundColor = [UIColor clearColor];
    self.duration.font = [UIFont systemFontOfSize:13 weight:0];
    [self.contentView addSubview:self.duration];
    self.duration.textAlignment = NSTextAlignmentRight;
//    self.duration.textColor = [UIColor lightGrayColor];
    [_duration release];
    
    
    
    //收藏按钮
    self.nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nextBtn.frame = CGRectMake(WIDTH*0.88, self.newsPic.frame.origin.y + self.newsPic.frame.size.height/2-HEIGHT*0.05, WIDTH*0.08, WIDTH*0.14);
    self.nextBtn.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.nextBtn];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(self.newsPic.frame.origin.x + self.newsPic.frame.size.width + 10, self.newsPic.frame.origin.y + self.newsPic.frame.size.height + 9, WIDTH - self.newsPic.frame.origin.x + self.newsPic.frame.size.width + 10, 0.5)];
//    lineView.backgroundColor = [UIColor lightGrayColor];
    [self.contentView addSubview:lineView];
    [lineView release];
}

-(void)setAlbumModel:(LJHAlbumModel *)albumModel
{
    if (_albumModel != albumModel) {
        [_albumModel release];
        _albumModel = [albumModel retain];
    }
    
    NSURL *url = [NSURL URLWithString:_albumModel.albumCoverUrl290];
    [self.newsPic sd_setImageWithURL:url placeholderImage:PLACEHOLDER];
    self.newsTitle.text = _albumModel.title;
    self.newsDigest.text = _albumModel.intro;
    self.contentCount.text = [NSString stringWithFormat:@"%.2f万", (CGFloat)_albumModel.playsCounts/10000];
    [self.contentCount sizeToFit];
    self.duration.text = [NSString stringWithFormat:@"%@集", _albumModel.tracksCounts];
    [self.duration sizeToFit];
    
    if (albumModel.isFavor) {
        [self.nextBtn setImage:[UIImage imageNamed:@"find_album_fav_n"] forState:UIControlStateNormal];
        
    }
    else
    {
        [self.nextBtn setImage:[UIImage imageNamed:@"find_album_unfav_n"] forState:UIControlStateNormal];
    }
    
    [self.nextBtn addTarget:self action:@selector(favorAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)favorAction:(UIButton *)btn
{
    if (_albumModel.isFavor) {
        [[LJHFMData shareFMDB] deleteByAlbumId:self.albumModel.myId];
        [self.nextBtn setImage:[UIImage imageNamed:@"find_album_unfav_n"] forState:UIControlStateNormal];
        self.albumModel.isFavor = NO;
    }
    else{
        [[LJHFMData shareFMDB] insertAlbumData:self.albumModel];
        [self.nextBtn setImage:[UIImage imageNamed:@"find_album_fav_n"] forState:UIControlStateNormal];
        self.albumModel.isFavor = YES;
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
